import { useRouter } from "vue-router";
import { useTitle as usePageTitle } from "@vueuse/core";
import { useAppStoreWidthOut } from "@/store/modules/app.js";

import { REDIRECT_NAME } from "../../router/constant";
import { watch, unref } from "vue";

// 不存储的路由tag
const notStoreTags = ["/auth/"];

export const useTitle = () => {
  const { currentRoute } = useRouter();
  const pageTitle = usePageTitle();
  watch(
    [() => currentRoute.value.path],
    () => {
      const route = unref(currentRoute);
      if (route.name === REDIRECT_NAME) {
        return;
      }
      // 设置名称
      pageTitle.value = route?.meta?.title;
      // 设置路由信息

      useAppStoreWidthOut().routeName = route?.name;
      useAppStoreWidthOut().activePath = route?.path;
<<<<<<< HEAD
      console.log(route);
=======
      useAppStoreWidthOut().route = route;
      const { fullPath } = route;
      // true：当前路由不存
      const NoStore = notStoreTags.some(tag => fullPath.startsWith(tag));
      !NoStore && useAppStoreWidthOut().storeRouteTags(route);
>>>>>>> lz
    },
    { immediate: true }
  );
};
